package engine.view;

import java.io.Serializable;

import engine.Engine;
import engine.MADGL;
import engine.game.Level;
import engine.graphics.IRender;
import engine.graphics.Vertex2f;
import engine.objects.PhysicsObject;

public class Camera implements IRender, Serializable {

	private static final long serialVersionUID = 1L;
	
	private Vertex2f position ; 
	private final Level level ; 
	
	public Camera(Vertex2f pos, Level l){
		this.setPosition(pos);
		this.level = l ; 
	}

	public Camera(float x, float y, Level l){
		this(new Vertex2f(x,y), l);
	}
	
	public Vertex2f getPosition() {
		return position;
	}

	public void setPosition(Vertex2f position) {
		float x = position.getX() ; 
		float y = position.getY(); 
		if(x<Engine.WIDTH / 2) x = Engine.WIDTH / 2 ; 
		else if(x>this.level.WIDTH - Engine.WIDTH/2) x = this.level.WIDTH - Engine.WIDTH/2; 
		if(y<Engine.HEIGHT/2) y = Engine.HEIGHT / 2 ; 
		else if(y>this.level.HEIGHT - Engine.HEIGHT/2) y = this.level.HEIGHT - Engine.HEIGHT/2 ; 
		x = x - (Engine.WIDTH / 2) ; 
		y = y - (Engine.HEIGHT / 2) ; 
		this.position = new Vertex2f(x, y) ; 
	}

	public Level getLevel(){
		return this.level ; 
	}
	
	@Override
	public void update() {
	}

	@Override
	public void render() {
		MADGL.Translatef(position);
	}

	public void centerCameraToObject(PhysicsObject o){
		float x = (float) o.getX() ; 
		float y = (float) o.getY() ;
		this.setPosition(new Vertex2f(x,y));		
	}
	
}
